<?php

class Upload extends CI_Controller {

	public function __construct() {
		parent::__construct();

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->model('paper_model');
	}

	public function _remap($method, $params)
	{
		$u = Current_User::user();
		if(!$u) {
			$this->load->view('pages/user/logout', array('info' => '请先登录系统'));
		}else{
			$this->opera = $u->account;
			$role = Current_User::my_role();
			if($role == Current_User::$administrtor) $this->is_admin = true;
			else $this->is_admin = false;
			if (method_exists($this, $method))
		    {
		        return call_user_func_array(array($this, $method), $params);
		    }
			//$this->$method();
		}
	}
	
	public function index($error="") {
		$data['schools'] = $this->paper_model->get_school();
		$data['professions'] = $this->paper_model->get_profession();
		$data['levels'] = $this->paper_model->get_level();
		$data['error'] = $error;

		// load the view
		$this->load->view('pages/paper/upload', $data);
	}

	public function edit($file_id) {
		$row = $this->paper_model->get_paper_from_id($file_id)->row();
		log_message("debug","admin=".$this->is_admin."---opera=".$this->opera."---down=".$row->download_user."---upload=".$row->upload_user);
		if($this->is_admin || $this->opera==$row->download_user || $this->opera==$row->upload_user){
			if($file_id) {
				
				$data['schools'] = $this->paper_model->get_school();
				$data['professions'] = $this->paper_model->get_profession();
				$data['levels'] = $this->paper_model->get_level();
				$data['error'] = "";
				
				$paper = $this->db->select()->from('paper')->where('file_id', $file_id)->get()->row_object();
				$data['paperObj'] = $paper;
				$this->load->view('pages/paper/edit', $data);
			}
		}else {
			redirect("/home/nopower");
		}
	}
	
	public function del($file_id) {
		$row = $this->paper_model->get_paper_from_id($file_id)->row();
		log_message("debug","admin=".$this->is_admin."---opera=".$this->opera."---down=".$row->download_user."---upload=".$row->upload_user);
		if($this->is_admin || $this->opera==$row->download_user || $this->opera==$row->upload_user){
			if($file_id) {			
				$this->db->delete('paper', array('file_id' => $file_id)); 
				redirect("paper/paper");
			}
		}else {
			redirect("/home/nopower");
		}
	}

	public function upload_file() {

		if ($this->_submit_validate() === FALSE) {
			$this->index();
			return;
		}
		
		$file_name = $_POST['upload_file'];
		$file_md5 = md5($file_name);
		log_message('debug',"sdaada".$file_md5);

		//判断文件名是否重复
		$file_md5s = $this->paper_model->get_file_md5s();
		log_message("debug", "md5=".$file_md5."---md5s=".json_encode($file_md5s));
		if(in_array($file_md5, $file_md5s)){
			$this->index("文件名重复，请重新命名文件！");
			return;
		}

		//if(!is_dir($config['upload_path'])) mkdir($config['upload_path'], 0700);
		//$config['upload_path'] = '/data/papers/file/'.substr($file_md5,-2);
		$config['upload_path'] = '/data/papers/file/';
		//$config['file_name'] = $file_md5;
		$config['allowed_types'] = '*';
		$config['max_size'] = '60000';
		$config['overwrite'] = TRUE;
		
		//log_message('debug', "upload_path:".config['upload_path']);
		$this->load->library('upload', $config);
		//print_r($this->upload->data());

		if ( ! $this->upload->do_upload())
		{
			log_message('debug', "upload error");
		 	$error = $this->upload->display_errors();
		 	
		 	$this->index($error);
		} 
		else
		{
			log_message('debug', "upload ok");
			
		 	$upload_data = $this->upload->data();
			// update database
			
		 	//处理school
		 	$school_obj = $this->paper_model->get_school_by_id($this->input->post('school'));
		 	if($school_obj->id) $data['school_id'] = $school_obj->id;
		 	else {
		 		$data_school = array('name' => $this->input->post('school'), 'insert_time' => date("Y-m-d H:i:s"));
		 		$this->paper_model->set_school($data_school);
		 		$school_obj = $this->paper_model->get_school_by_id($this->input->post('school'));
		 		$data['school_id'] = $school_obj->id;
		 	}

		 	//profession
		 	$profession_obj = $this->paper_model->get_profession_by_id($this->input->post('profession'));
		 	if($profession_obj->id) $data['profession_id'] = $profession_obj->id;
		 	else {
		 		$data_profession = array('name' => $this->input->post('profession'), 'insert_time' => date("Y-m-d H:i:s"));
		 		$this->paper_model->set_profession($data_profession);
		 		$profession_obj = $this->paper_model->get_profession_by_id($this->input->post('profession'));
		 		$data['profession_id'] = $profession_obj->id;
		 	}

			$info = $this->upload->data();

			$data['file_name'] = $info["file_name"];
			
			$data['level_id'] = $this->input->post('level');
			$data['file_md5'] = $file_md5;
			$data['teacher'] = $this->input->post('teacher');
			$data['upload_user'] = $this->opera;
			$data['words'] = $this->input->post('words');

			$this->paper_model->save_paper($data);
		 	#$this->load->view('pages/paper/upload', $data);
		 	redirect('/paper/paper');

		}
		
	}

	public function modify($exit_file=false) {

		if ($this->_submit_validate() === FALSE) {
			$this->index();
			return;
		}
		
			// update database
			
			$data['school_id'] = $this->input->post('school');
			$data['level_id'] = $this->input->post('level');
			$data['profession_id'] = $this->input->post('profession');
			$data['teacher'] = $this->input->post('teacher');
			$data['upload_user'] = $this->opera;
			$data['words'] = $this->input->post('words');

			$this->paper_model->update_paper($exit_file, $data); 
		 	#$this->load->view('pages/paper/upload', $data);
		 	redirect('/paper/paper');
		
	}

	private function _submit_validate() {

		// validation rules
		$this->form_validation->set_rules('teacher', '指导老师',
			'required|min_length[2]|max_length[12]');
		$this->form_validation->set_rules('words', '字数',
			'trim|required|numeric');

		return $this->form_validation->run();

	}
}
